package com.kymatrix.mpo.plus.bpm.server.controller.definition;

import com.kymatrix.mpo.plus.bpm.server.model.entity.definition.BpmUserGroupEntity;
import com.kymatrix.mpo.plus.bpm.server.model.form.group.PageQueryBpmUserGroupReq;
import com.kymatrix.mpo.plus.bpm.server.model.form.group.QueryBpmUserGroupResp;
import com.kymatrix.mpo.plus.bpm.server.model.form.group.SaveBpmUserGroupReq;
import com.kymatrix.mpo.plus.bpm.server.model.form.group.UpdateBpmUserGroupReq;
import com.kymatrix.mpo.plus.bpm.server.service.BpmUserGroupService;
import com.kymatrix.mpo.plus.common.business.common.PageResp;
import com.kymatrix.mpo.plus.common.business.common.ResultResp;
import com.kymatrix.mpo.plus.common.enums.EnableTypeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import javax.validation.Valid;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


@Api(tags = "BPM 用户组相关请求处理类")
@RestController
@Validated
public class BpmUserGroupController {

    @Resource
    private BpmUserGroupService userGroupService;

    @PostMapping("/bpm/user-group/create")
    @ApiOperation(value = "创建用户组")
    public ResultResp<String> createUserGroup(@Valid @RequestBody SaveBpmUserGroupReq req) {
        return ResultResp.ok(userGroupService.createUserGroup(req));
    }

    @PutMapping("/bpm/user-group/update")
    @ApiOperation(value = "更新用户组")
    public ResultResp<Boolean> updateUserGroup(@Valid @RequestBody UpdateBpmUserGroupReq req) {
        userGroupService.updateUserGroup(req);
        return ResultResp.ok(true);
    }

    @DeleteMapping("/bpm/user-group/delete")
    @ApiOperation(value = "删除用户组")
    public ResultResp<Boolean> deleteUserGroup(@RequestParam("id") String id) {
        userGroupService.deleteUserGroup(id);
        return ResultResp.ok(true);
    }

    @GetMapping("/bpm/user-group/get")
    @ApiOperation(value = "获得用户组")
    public ResultResp<QueryBpmUserGroupResp> getUserGroup(@RequestParam("id") String id) {
        BpmUserGroupEntity userGroup = userGroupService.getUserGroup(id);
        return ResultResp.ok(QueryBpmUserGroupResp.fromEntity(userGroup));
    }

    @GetMapping("/bpm/user-group/page")
    @ApiOperation(value = "获得用户组分页")
    public ResultResp<PageResp<QueryBpmUserGroupResp>> getUserGroupPage(@Valid PageQueryBpmUserGroupReq req) {
        PageResp<BpmUserGroupEntity> pageResult = userGroupService.getUserGroupPage(req);
        PageResp<QueryBpmUserGroupResp> resp = new PageResp<>();
        resp.setTotal(pageResult.getTotal());
        for (BpmUserGroupEntity entity : pageResult.getList()) {
            resp.getList().add(QueryBpmUserGroupResp.fromEntity(entity));
        }
        return ResultResp.ok(resp);
    }

    @GetMapping("/bpm/user-group/simple-list")
    @ApiOperation(value = "获取用户组精简信息列表", notes = "只包含被开启的用户组，主要用于前端的下拉选项")
    public ResultResp<List<QueryBpmUserGroupResp>> getUserGroupSimpleList() {
        List<BpmUserGroupEntity> list = userGroupService.getUserGroupListByStatus(EnableTypeEnum.ENABLE.getCode());
        List<QueryBpmUserGroupResp> res = new ArrayList<>();
        for (BpmUserGroupEntity entity : list) {
            res.add(QueryBpmUserGroupResp.fromEntity(entity));
        }
        return ResultResp.ok(res);
    }

}
